package cn.wisco.util;

import java.util.StringTokenizer;


public class StringAssistant {
  public StringAssistant() {
  }

  public static String getStringClear(String s) {
//	  int n = s.indexOf("");
//	  System.out.println("str: " + s +" ;n=" + n);
//	  if(n == 0){
//		 return getStringClear(s.substring(s.indexOf("")+1),""); 
//	  }else{
		 return getStringClear(s, ""); 
//	  }
  }

  public static String getStringClear(String s, String nullReturn) {
    if (s == null || s.trim().equals("") || s.trim().equals("null")) {
      return nullReturn;
    }
    return s.trim();
  }

  public static String ISO885912GBK(String s) {
    if (s == null || s.trim().equals("") || s.trim().equals("null")) {
      return s;
    }

    try {
      return new String(s.getBytes("ISO8859_1"), "GBK");
    }catch (Exception e) {
      return "";
    }
  }

  public static int String2Int(String s) {
    if (s == null || s.trim().equals("") || s.trim().equals("null")) {
      return 0;
    }

    try {
      return Integer.parseInt(findReplace(s, ",", ""));
    }
    catch (Exception e) {
      return 0;
    }
  }

  public static long String2Long(String s) {
    if (s == null || s.trim().equals("") || s.trim().equals("null")) {
      return 0L;
    }

    try {
      return Long.parseLong(findReplace(s, ",", ""));
    }
    catch (Exception e) {
      return 0L;
    }
  }

  public static double String2Double(String s) {
    if (s == null || s.trim().equals("") || s.trim().equals("null")) {
      return 0D;
    }

    try {
      return Double.parseDouble(findReplace(s, ",", ""));
    }
    catch (Exception e) {
      return 0D;
    }
  }

  public static String clearEndsWith(String s, String endsWith) {
    if (s == null || endsWith == null)
      return s;
    if (s.equals(""))
      return "";

    if (s.endsWith(endsWith)) {
      return s.substring(0, s.length() - endsWith.length());
    }
    else {
      return s;
    }
  }

  public static String double2String(double d) {
    java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
    return findReplace(nf.format(d), ",", "");
    //return nf.format(d);
  }

  public static String double2StringComma(double d) {
    java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
    return nf.format(d);
  }

  public static String long2String(long d) {
    java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
    return findReplace(nf.format(d), ",", "");
  }

  public static String findReplace(String aSrc, String aSub,
                                   String aReplaceWith) {
    String dest = aSrc;
    if (dest == null || dest.equals(""))
      return "";
    int fromIndex = 0;
    while ( (fromIndex = dest.indexOf(aSub, fromIndex)) != -1) {
      dest = dest.substring(0, fromIndex) + aReplaceWith +
          dest.substring(fromIndex + aSub.length());
      fromIndex += /*1*/ aReplaceWith.length();
    }
    return dest;
  }

  public static int getFieldCount(String aSrc, String aDelim) {
    if (aSrc == null || aSrc.equals(""))
      return 0;

    int fromIndex = 0;
    int fieldCount = 1;
    while ( (fromIndex = aSrc.indexOf(aDelim, fromIndex)) != -1) {
      fromIndex++;
      fieldCount++;
    }
    return fieldCount;
  }

  public static String getField(String aSrc, String aDelim, long aFieldNum) {
    if (aSrc == null || aFieldNum < 0)
      return null;

    int beginIndex = 0;
    int endIndex = 0;
    int fieldNum = 0;

    if (aSrc.indexOf(aDelim) == -1 && aFieldNum == 0)
      return aSrc;

    if (aFieldNum >= getFieldCount(aSrc, aDelim))
      return "";

    while (aSrc.indexOf(aDelim, endIndex) != -1) {
      endIndex = aSrc.indexOf(aDelim, endIndex);
      if (fieldNum == aFieldNum) {
        break;
      }
      endIndex += aDelim.length();
      beginIndex = endIndex;
      fieldNum++;
    }

    if (beginIndex == endIndex && aSrc.indexOf(aDelim, endIndex) == -1)
      endIndex = aSrc.length();

    return aSrc.substring(beginIndex, endIndex);
  }

  public static String cutSpecialTail(String s){
      if (s == null || s.trim().equals("") || s.trim().equals("null")) {
          return "&nbsp;";
      }

      int i = s.lastIndexOf(":");
      if(i > 10){
          return s.substring(0, i);
      }else{
          return s;
      }

  }

  public static String first2User(String s){
      if (s == null || s.trim().equals("") || s.trim().equals("null")) {
          return "&nbsp;";
      }

      String s2 = s;

      int i = s.indexOf(",");
      if(i > 0){
          int j = s.indexOf(",", i+1);
          if(j > 0){
              s2 = s.substring(0, j) + "...";
          }
          s2 = findReplace(s2, ",",",<br>");
      }
      return s2;
  }

  public static String limitShowString(String s, int limit){
      if (s == null || s.trim().equals("") || s.trim().equals("null")) {
          return "&nbsp;";
      }

      if(s.length() > limit){
          return s.substring(0, limit) + "...";
      }else{
          return s;
      }

  }

  public static String str_replace(String from,String to,String source) {
	    StringBuffer bf= new StringBuffer("");
	   
	    StringTokenizer st = new StringTokenizer(source,from,true);
	    while (st.hasMoreTokens()) {
	      String tmp = st.nextToken();
	      if(tmp.equals(from)) {
	        bf.append(to);
	      } else {
	        bf.append(tmp);
	      }
	    }
	    return bf.toString();
}


}
